package pms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import pms.dao.EventDao;
import pms.dao.KundeDao;
import pms.dao.TheaterDao;
import pms.om.Kunde;


/** 
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class EditKunde extends AbstractController {
	private KundeDao kundeDao;
	

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	private EventDao eventDao;
	
	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}
	
	public EventDao getEventDao() {
		return eventDao;
	}
	
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("privileged", httpServletRequest.isUserInRole("admin"));
		try {
			int eventId = Integer.parseInt(httpServletRequest.getParameter("eventId"));
			// wenn id nicht geparsed werden kann, dann wird ein neues Objekt angelegt
			mv.addObject("title", "Veranstaltung buchen");
			mv.addObject("message", "Tragen Sie hier Ihre Kundendaten ein.");
			mv.addObject("eventId", eventId);
			mv.addObject("event", eventDao.findById(eventId));
			mv.addObject(new Kunde());
			//mv.addObject(theaterDao.findById(id));
		} catch (NumberFormatException e) {
			mv.addObject("title", "Neues Theater anlegen");
			mv.addObject("message", "Sie befinden sich im Admin-Bereich. Hier können Sie eine neues Theater anlegen.");
			//mv.addObject(new Theater("Neues Theater"));
		}
		mv.setViewName("edit-kunde");
		return mv;
	}

}
